 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=174 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:08 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Test using main method
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='SplashScreen,main,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Test using main method</div>

<div class='main-body'>
 
<br>The <a href='http://java.sun.com/docs/books/jls/second_edition/html/execution.doc.html#44444'><tt>main</tt></a> 
method may be used to test the operation of a class.
It should probably be used informally, since it lacks the features of a
more complete testing tool such as <a href="TopicActiond5a5-2.html">JUnit</a>.
<p>When finished testing, <tt>main</tt> can simply be deleted, or it may
be kept for future needs. If kept, it is probably a good idea to change
its scope to <tt>private</tt>, in order to avoiding polluting the class
API with a method intended only for development. Of course, if <tt>main</tt>
needs to be run again later, you will need to temporarily change its scope back to <tt>public</tt>.

<p><b>Example</b>

<p>Here, <tt>SplashScreen.main</tt> exercises the display of a splash screen.
It is placed at the very bottom of the class, since it is intended only
as a development tool. The informal character of this <tt>main</tt> is
reflected in the call to <tt>Thread.sleep</tt>, which simply wastes time
for a few seconds. Doing this in the real application would be silly,
since a splash screen's purpose is to give the illusion of rapid feedback.
In the real application, <tt>SplashScreen</tt> is disposed of not after
a fixed number of seconds, but rather after the appearance of the primary
window.
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.stocks.main;

<span class='keyword'>import</span> java.awt.*;
<span class='keyword'>import</span> java.net.URL;
<span class='keyword'>import</span> java.awt.image.ImageObserver;

<span class='comment'>/**
* Present a simple graphic to the user upon launch of the application, to 
* provide a faster initial response than is possible with the main window.
* 
* &lt;P&gt;Adapted from an 
* &lt;a href=http://developer.java.sun.com/developer/qow/archive/24/index.html&gt;item&lt;/a&gt; 
* on Sun's Java Developer Connection.
*
* &lt;P&gt;This splash screen appears within about 2.5 seconds on a development 
* machine. The main screen takes about 6.0 seconds to load, so use of a splash 
* screen cuts down the initial display delay by about 55 percent. 
*/</span>
<span class='keyword'>final</span> <span class='keyword'>class</span> SplashScreen <span class='keyword'>extends</span> Frame {

  <span class='comment'>/**
  * Construct using an image for the splash screen.
  *  
  * @param aImageId must have content, and is used by  
  * {@link Class#getResource(java.lang.String)} to retrieve the splash screen image.
  */</span>
  SplashScreen(String aImageId) {
    <span class='comment'>/* 
    * Implementation Note
    * Args.checkForContent is not called here, in an attempt to minimize 
    * class loading.
    */</span>
    <span class='keyword'>if</span> ( aImageId == <span class='keyword'>null</span> || aImageId.trim().length() == <span class='literal'>0</span> ){
      <span class='keyword'>throw</span> <span class='keyword'>new</span> IllegalArgumentException(<span class='literal'>"Image Id does not have content."</span>);
    }
    fImageId = aImageId;
  }
   
  <span class='comment'>/**
  * Show the splash screen to the end user.
  *
  * &lt;P&gt;Once this method returns, the splash screen is realized, which means 
  * that almost all work on the splash screen should proceed through the event 
  * dispatch thread. In particular, any call to &lt;tt&gt;dispose&lt;/tt&gt; for the 
  * splash screen must be performed in the event dispatch thread.
  */</span>
  <span class='keyword'>void</span> splash(){
    initImageAndTracker();
    setSize(fImage.getWidth(NO_OBSERVER), fImage.getHeight(NO_OBSERVER));
    center();
    
    fMediaTracker.addImage(fImage, IMAGE_ID);
    <span class='keyword'>try</span> {
      fMediaTracker.waitForID(IMAGE_ID);
    }
    <span class='keyword'>catch</span>(InterruptedException ex){
      System.out.println(<span class='literal'>"Cannot track image load."</span>);
    }

    SplashWindow splashWindow = <span class='keyword'>new</span> SplashWindow(<span class='keyword'>this</span>,fImage);
  }
  
  <span class='comment'>// PRIVATE//
</span>  <span class='keyword'>private</span> <span class='keyword'>final</span> String fImageId;
  <span class='keyword'>private</span> MediaTracker fMediaTracker;
  <span class='keyword'>private</span> Image fImage;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> ImageObserver NO_OBSERVER = <span class='keyword'>null</span>; 
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> <span class='keyword'>int</span> IMAGE_ID = <span class='literal'>0</span>;

  <span class='keyword'>private</span> <span class='keyword'>void</span> initImageAndTracker(){
    fMediaTracker = <span class='keyword'>new</span> MediaTracker(<span class='keyword'>this</span>);
    URL imageURL = SplashScreen.<span class='keyword'>class</span>.getResource(fImageId);
    fImage = Toolkit.getDefaultToolkit().getImage(imageURL);
  }

  <span class='comment'>/**
  * Centers the frame on the screen.
  *
  *&lt;P&gt;This centering service is more or less in {@link hirondelle.stocks.util.ui.UiUtil}; 
  * this duplication is justified only because the use of  
  * {@link hirondelle.stocks.util.ui.UiUtil} would entail more class loading, which is 
  * not desirable for a splash screen.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>void</span> center(){
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
    Rectangle frame = getBounds();
    setLocation((screen.width - frame.width)/<span class='literal'>2</span>, (screen.height - frame.height)/<span class='literal'>2</span>);
  }
 
  <span class='keyword'>private</span> <span class='keyword'>class</span> SplashWindow <span class='keyword'>extends</span> Window {
    SplashWindow(Frame aParent, Image aImage) {
       <span class='keyword'>super</span>(aParent);
       fImage = aImage;
       setSize(fImage.getWidth(NO_OBSERVER), fImage.getHeight(NO_OBSERVER));
       Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
       Rectangle window = getBounds();
       setLocation((screen.width - window.width) / <span class='literal'>2</span>,(screen.height - window.height)/<span class='literal'>2</span>);
       setVisible(<span class='keyword'>true</span>);
    }
    <span class='keyword'>public</span> <span class='keyword'>void</span> paint(Graphics graphics) {
      <span class='keyword'>if</span> (fImage != <span class='keyword'>null</span>) {
        graphics.drawImage(fImage,<span class='literal'>0</span>,<span class='literal'>0</span>,<span class='keyword'>this</span>);
      }
    }
    <span class='keyword'>private</span> Image fImage;
  }
  
  <span class='comment'>/**
  * Developer test harness shows the splash screen for a fixed length of 
  * time, without launching the full application.
  */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main(String... aArgs){
    SplashScreen splashScreen = <span class='keyword'>new</span> SplashScreen(<span class='literal'>"StocksMonitor.gif"</span>);
    splashScreen.splash();
    <span class='keyword'>try</span> {
      Thread.sleep(<span class='literal'>2000</span>);
    }
    <span class='keyword'>catch</span>(InterruptedException ex) {
      System.out.println(ex);
    }
    System.exit(<span class='literal'>0</span>);
  }
}
 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicActiond5a5-2.html?Id=33'>Use a testing framework (JUnit)</a> <br>
 
  
  <a href='TopicAction028c-2.html?Id=149'>Splash screen</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='174'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=174 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:08 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
